package com.example.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.example.myapplication.util.CommandExecution;
import com.example.myapplication.util.LogUtil;

/**
 * 程序卸载，安装，更新的广播监听器
 * */
public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        String packageName = intent.getDataString().substring(8);
        LogUtil.d("com.example.myapplication.BootReceiver onReceive():接收到Intent.getAction() = "+intent.getAction()+" , 包名 = "+intent.getDataString());

        /**
         * 接收安装广播
         * android.intent.action.PACKAGE_ADDED
         * */
        if (action.equals(Intent.ACTION_PACKAGE_ADDED)) {
            LogUtil.d("com.example.myapplication.BootReceiver onReceive():安装了:" + packageName + "包名的程序");
//            if (SystemCtrlUtil.rootStartApk("com.choryan.quan.videowzproject", ".ActivityStart")) {
//                LogUtil.d("静默安装后启动APP成功");
//            } else {
//                LogUtil.e("静默安装后启动APP失败！！！");
//            }
            packageName = "com.example.myapplication";
            String cmd = "am start -n " + packageName + "/" + ".InstallActivity";
            CommandExecution.execCommand(cmd,true);
        }

        /**
         * 接收卸载广播
         * android.intent.action.PACKAGE_REMOVED
         * */
        if (action.equals(Intent.ACTION_PACKAGE_REMOVED)) {
            LogUtil.d("com.example.myapplication.BootReceiver onReceive():卸载了:" + packageName + "包名的程序");

        }

        /**
         * 接收更新广播
         * android.intent.action.PACKAGE_REPLACED
         * */
        if(action.equals(Intent.ACTION_PACKAGE_REPLACED)) {
            LogUtil.d("com.example.myapplication.BootReceiver onReceive():更新了:" + packageName + "包名的程序，context.getPackageName()="+context.getPackageName());

            // 更新的软件包名是否和我的工程一致
            if(packageName.equals(context.getPackageName())){
                //更新完后打开
                Intent it = new Intent();
                it.setClassName(packageName, packageName + ".MainActivity");    //启动类
                it.setAction("android.intent.action.MAIN");                             //首个启动类action
                it.addCategory("android.intent.category.LAUNCHER");                     //放入程序列表
                //it.addCategory("android.intent.category.HOME");                           //作为桌面，Home键打开，可做启动默认程序
                it.addCategory("android.intent.category.DEFAULT");                      //隐式打开，如果没main可有，如果main可有可无
                it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(it);
            }

        }

        /**
         * 接收重启广播
         * android.intent.action.PACKAGE_REPLACED
         * */
        if(action.equals(Intent.ACTION_PACKAGE_RESTARTED)) {
            LogUtil.d("com.example.myapplication.BootReceiver onReceive():重启了:" + packageName + "包名的程序");
        }

        /**
         * 接收开机广播
         * android.intent.action.BOOT_COMPLETED
         * */
        if(action.equals(Intent.ACTION_BOOT_COMPLETED)) {
            LogUtil.d("com.example.myapplication.BootReceiver onReceive():仪器开机，开启了:" + packageName + "包名的程序");
        }

    }
}